package com.cll.pmis.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cll.pmis.common.Constants;
import com.cll.pmis.common.exception.TransactionException;
import com.cll.pmis.entity.*;
import com.cll.pmis.mapper.DrugMapper;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chenliangliang
 * @date 2018/7/1
 */
@Component
public class MsgQueueConsumer {


    private IInputService inputService;
    private IDrugService drugService;
    private IInputDrugService inputDrugService;
    private IPrescribeService prescribeService;
    private IOutputService outputService;

    protected MsgQueueConsumer(IInputDrugService inputDrugService, IInputService inputService, IDrugService drugService,
                               IPrescribeService prescribeService, IOutputService outputService) {
        this.drugService = drugService;
        this.inputDrugService = inputDrugService;
        this.inputService = inputService;
        this.prescribeService = prescribeService;
        this.outputService = outputService;
    }


    @JmsListener(destination = Constants.INPUT_DRUG_QUEUE)
    public void inputDrug(String msg) {
        System.out.println("msg-service: inputDrug:"+msg);
        Integer inputId = Integer.valueOf(msg);
        List<InputDrug> inputDrugs = inputDrugService.selectList(new EntityWrapper<InputDrug>()
                .eq("input_id", inputId));

        if (inputDrugs == null || inputDrugs.isEmpty()) {
            return;
        }
        List<Drug> drugs = new LinkedList<>();
        Drug drug;
        for (InputDrug inputDrug : inputDrugs) {
            drug = new Drug();
            drug.setName(inputDrug.getName());
            drug.setNum(inputDrug.getNum());
            drug.setCostPrice(inputDrug.getCostPrice());
            drug.setSellPrice(inputDrug.getSellPrice());
            drug.setProductTime(inputDrug.getProductTime());
            drug.setOverTime(inputDrug.getOverTime());
            drug.setProvider(inputDrug.getProvider());
            drug.setSpecification(inputDrug.getSpecification());
            drug.setUnit(inputDrug.getUnit());
            drug.setCreateTime(new Date());
            drug.setUpdateTime(new Date());
            drugs.add(drug);
        }
        if (drugService.insertBatch(drugs)) {
            //更新入库表状态
            Input input = new Input();
            input.setId(inputId);
            input.setUpdateTime(new Date());
            input.setStatus(Constants.INPUT_STATUS_INPUTED);
            inputService.updateById(input);
        }
    }


    @JmsListener(destination = Constants.OUTPUT_DRUG_QUEUE)
    @Transactional(rollbackFor = TransactionException.class)
    public void outputDrug(String msg) {
        Integer outputId = Integer.valueOf(msg);

        List<Prescribe> prescribes = prescribeService.getMapper().queryOutputDrug(outputId);

        if (prescribes == null || prescribes.isEmpty()) {
            return;
        }
        DrugMapper drugMapper = drugService.getMapper();
        for (Prescribe outputDrug : prescribes) {
            int res = drugMapper.deductNum(outputDrug.getDrugId(), outputDrug.getNum());
            if (res != 1) {
                throw new TransactionException("id为 " + outputDrug.getDrugId() + " 的药品出库失败");
            }
        }
        //更新出库表状态
        Output output = new Output();
        output.setId(outputId);
        output.setUpdateTime(new Date());
        output.setStatus(Constants.OUTPUT_STATUS_OUTPUTED);
        outputService.updateById(output);
    }


//    @JmsListener(destination = Constants.OUTPUT_QUEUE)
//    public void output(String orderIdStr){
//        Integer orderId=Integer.valueOf(orderIdStr);
//
//        List<Prescribe> prescribes = prescribeService.selectList(new EntityWrapper<Prescribe>().eq("order_id", orderId));
//        if (prescribes==null||prescribes.isEmpty()){
//            return;
//        }
//
//        Output output = outputService.selectOne(new EntityWrapper<Output>().eq("order_id", orderId));
//        if (output.getStatus()!=Constants.OUTPUT_STATUS_UNCHECK){
//            return;
//        }
//        List<OutputDrug> outputDrugs=new LinkedList<>();
//        OutputDrug outputDrug;
//        for (Prescribe prescribe:prescribes){
//            outputDrug=new OutputDrug();
//            outputDrug.setDrugId(prescribe.getDrugId());
//            outputDrug.setNum(prescribe.getNum());
//            outputDrug.setOutputId(output.getId());
//            outputDrug.setCreateTime(new Date());
//        }
//
//
//    }
}
